package com.itheima.springbootwebbtalis.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.springbootwebbtalis.commons.DeletionIsNotAllowed;
import com.itheima.springbootwebbtalis.mapper.ClazzMapper;
import com.itheima.springbootwebbtalis.mapper.StudentMapper;
import com.itheima.springbootwebbtalis.pojo.Clazz;
import com.itheima.springbootwebbtalis.pojo.ClazzQueryParam;
import com.itheima.springbootwebbtalis.pojo.PageResult;
import com.itheima.springbootwebbtalis.service.ClazzService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {
    @Autowired
    private ClazzMapper clazzMapper;
    @Autowired
    private StudentMapper  studentMapper;

    @Override
    public void classServiceImplAdd(Clazz clazz) {
        clazz.setBeginDate(LocalDate.now());
        clazz.setEndDate(LocalDate.now());
        clazzMapper.clazzMapperAdd(clazz);
    }

    @Override
    public List<Clazz> ServiceShowAll() {
        List<Clazz> clazzes = clazzMapper.clazzMapperShowAll();
        return clazzes;
    }

    @Override
    public Clazz ServiceShowById(Integer id) {
        Clazz clazz = clazzMapper.clazzMapperShowById(id);

        return clazz;}

    @Override
    public void serviceDelete(Integer id) {
        Integer count=studentMapper.deleteByClazzId(id);
        if(count != null){
            throw new DeletionIsNotAllowed("该班级有学生，不能删除");
        }

        clazzMapper.clazzMapperDeteleById(id);
    }

    @Override
    public void ServiceUpdate(Clazz clazz) {
        clazzMapper.clazzMapperUpdate(clazz);
    }

    @Override
    public PageResult page(ClazzQueryParam param) {
        PageHelper.startPage(param.getPage(),param.getPageSize());
        Page<Clazz> page= clazzMapper.selectByPage(param);
        return new PageResult(page.getTotal(),page.getResult());

    }


}
